package com.byq.mm.smartlib.engine;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.byq.mm.abstractx.engine.AccountManager;
import com.byq.mm.abstractx.engine.DefinationConfig;

public class AndroidAccountManager implements AccountManager, DefinationConfig {
	public AndroidAccountManager(Context ctx) {
		super();
		this.ctx = ctx;
		prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
	}
	static Map<String, String> hosts = new HashMap<String, String>();
	static{
		hosts.put("gmail.com", "talk.google.com");
		hosts.put("xmpp.jp", "xmpp.jp");
		hosts.put("localhost", "localhost");
	}
	private SharedPreferences prefs;
	Context ctx;

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getUserName()
	 */
	public String getJid() {
		String service = getService();
		String user = getUser();
		if(user == null)
			return null;
		return user + "@" + service;
	}

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getHost()
	 */
	public String getHost() {
		String service = getService();
		return service;
	}

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getUser()
	 */
	public String getUser() {
		return prefs.getString("prefJabberIdKey", "byq8888");
		
	}

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getPassword()
	 */
	public String getPassword() {
		return prefs.getString("prefPasswordKey", "13311509083");
	}

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getPort()
	 */
	public int getPort() {
		return Integer.parseInt(prefs.getString("prefServerPortKey", "5222"));
	}

	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getService()
	 */
	public String getService() {
		return "10.20.4.146";
		//return (prefs.getString("prefServerKey", "xmpp.jp")).trim();
	}
	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#tlskeyEnabled()
	 */
	public boolean tlskeyEnabled(){
		return prefs.getBoolean("prefEnableTlsKey", true);
	}
	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#saslkeyEnabled()
	 */
	public boolean saslkeyEnabled(){
		return 	prefs.getBoolean("prefEnableSaslKey", true);
	}
	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#getResourceKey()
	 */
	public String getResourceKey(){
		return prefs.getString("prefResourceKey", "Android");
	}
	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#logMessageKey()
	 */
	public boolean logMessageKey(){
		return prefs.getBoolean("prefLogMessagesKey", true);
	}
	/* (non-Javadoc)
	 * @see com.byq.mm.AccountManager#valid()
	 */
	public boolean valid(){
		String s = getJid();
		if(s == null)
			return false;
		
		
	//	String ps = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";

	//	boolean match = Pattern.matches(ps, s);
	//	if(!match)
	//		return false;

		
		String password = getPassword();
		if(password == null || password.equals(""))
			return false;
		String host = getHost();
		if(host == null || host.equals(""))
			return false;
		
		return true;
	}


	@Override
	public String forOffer() {
		return prefs.getString("prefForOfferKey", "accept");
	}

	@Override
	public String forRequest() {
		// TODO Auto-generated method stub
		return prefs.getString("prefForRequestKey", "agree");
	}

}
